/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "蒙皮骨骼.h"


C_蒙皮节点::C_蒙皮节点(S_设备环境& ctx) : C_节点基类("蒙皮节点") {
	m_Ctx = ctx;
	m_Name = L"蒙皮";

	C_插座基类* socket;
	DEF_创建物体插座_I(L"对象");
	DEF_创建骨架插座_I(L"骨骼");
	DEF_创建物体插座_O(L"对象");

	m_权重编辑 = f_alloc_BoolProp(nullptr, L"编辑权重");
	m_权重编辑.m_私有 = true;

	m_自动权重 = f_alloc_BoolProp(nullptr, L"自动权重");
	m_自动权重.m_私有 = true;

	m_封套颜色长度 = f_alloc_F32Prop(nullptr, L"封套颜色长度");
	m_封套颜色长度.m_私有 = true;
	f_prop_F32(m_封套颜色长度) = 0.3;
}

C_蒙皮节点::~C_蒙皮节点() {
}

bool C_蒙皮节点::f_update() {
	auto* ob = DEF_物体插座数据(f_get输入插座(1), 0);
	auto* 骨架 = DEF_骨架插座数据(f_get输入插座(2), 0);

	//auto& outOb = *DEF_物体插座数据(f_get输出插座(1), 0);
	if (ob) {
		auto* mesh = f_ob_fromMesh(ob, 0);
		//f_buf_resize(mesh->m_蒙皮权重, mesh->m_顶点->m_Size * 4);
		//f_buf_resize(mesh->m_顶点对应骨骼ID, mesh->m_顶点->m_Size);
		//f_buf_resize(mesh->m_蒙皮权重, mesh->m_顶点->m_Mem.m_大小 * 4);
		//f_buf_resize(mesh->m_顶点对应骨骼ID, mesh->m_顶点->m_Mem.m_大小);

		if (骨架) {
			ob->m_骨架 = 骨架;
			
			f_surface_计算自动权重(mesh, f_ob_get变换矩阵(ob), 骨架, f_prop_F32(m_封套颜色长度));
			if (f_prop_Bool(m_自动权重)) {

			}
		}
		else {
			ob->m_骨架 = nullptr;
			goto To_清除权重;
		}

		f_get输出插座(1)->f_setData(ob);
	}
	else {
		goto To_清除权重;
	}

	
	//if (f_prop_Bool(S_节点数据状态::g_打开编辑模式)) {
	//}
	//if (f_prop_Bool(m_权重编辑)) {
	//}
	

	return false;

	To_清除权重:
	f_get输出插座(1)->f_setData((void*)nullptr); 
	return false;
}

void C_蒙皮节点::f_读取(FILE* f) {
	C_节点基类::f_读取(f);
	f_prop_Load(m_自动权重, f);
	f_prop_Load(m_封套颜色长度, f);
}

void C_蒙皮节点::f_写入(FILE* f) {
	C_节点基类::f_写入(f);
	f_prop_Save(m_自动权重, f);
	f_prop_Save(m_封套颜色长度, f);
}

C_节点基类* f_node_加载蒙皮节点(S_设备环境& ctx, FILE* f) {
	C_蒙皮节点* node = new C_蒙皮节点(ctx);

	return node;
}

void f_node_保存蒙皮节点(C_节点基类* n, FILE* f) {
	C_蒙皮节点* node = dynamic_cast<C_蒙皮节点*>(n);
}













